package com.ruoyi.ex.domain;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.pv.domain.PvTrainingClass;
import lombok.*;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 题库对象 ex_question_bank
 *
 * @author zzx
 * @date 2023-03-23
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@ToString
public class ExQuestionBank extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    private Long id;

    /**
     * 培训班编号
     */
    private Long classId;
    private PvTrainingClass clazz;

    /**
     * 初复训标识
     */
    private Integer trainFlag;

    /**
     * 唯一标识
     */
    private String uniqueFlag;

    /**
     * 名称
     */
    @Excel(name = "名称")
    private String name;

    /**
     * 描述
     */
    @Excel(name = "描述")
    private String info;
    private JSONArray appointDateRange;

    /**
     * 类别
     */
    @Excel(name = "类别")
    private String category;

    /**
     * 状态
     */
    @Excel(name = "状态")
    private Integer status;

    /**
     * 是否是理论考试
     */
    private Integer isEx;

    /**
     * 题目总数
     */
    private Integer total;

    /**
     * 机构编号
     */
    private Long deptId;

    /**
     * 预留字段1
     */
    private String reservedOne;
    private JSONArray reservedOneList;
    private Set<String> exDateRange;

    /**
     * 预留字段2
     */
    private String reservedTwo;
    private JSONArray reservedTwoList;

    /**
     * 是否可以开始考试字段
     * 0:可以考试
     * 1:尚未开始
     * 2:已结束
     */
    private Integer ok;

    /**
     * 是否可以预约
     */
    private Boolean applyOk;

    public void setInfo(String info) {
        this.info = info;
        if (JSONUtil.isTypeJSONArray(info)) {
            this.appointDateRange = JSONUtil.parseArray(info);
            this.applyOk = DateUtil.isIn(DateUtil.date(),
                    this.appointDateRange.getDate(0),
                    this.appointDateRange.getDate(1));
        }
    }

    public void setReservedOne(String reservedOne) {
        this.reservedOne = reservedOne;
        if (JSONUtil.isTypeJSON(reservedOne)) {
            JSONArray dates = JSONUtil.parseArray(reservedOne);
            this.reservedOneList = dates;
            Date startTime = this.reservedOneList.getDate(0);
            Date endTime = this.reservedOneList.getDate(1);
            Date appointStartTime = this.appointDateRange.getDate(0);
            Date appointEndTime =this.appointDateRange.getDate(1);
            DateTime now = DateUtil.date();
            this.ok = now.before(startTime) ? 1 : now.after(endTime) ? 2 : 0;
            if (DateUtil.isIn(now, appointStartTime, appointEndTime)){
                this.exDateRange = new HashSet<String>(){{
                    addAll(DateUtil.rangeToList(startTime, endTime, DateField.DAY_OF_MONTH).stream().map(DateTime::toDateStr).collect(Collectors.toList()));
                }};
            }else {
                this.exDateRange = new HashSet<>();
            }
        }
    }

    public void setReservedTwo(String reservedTwo) {
        this.reservedTwo = reservedTwo;
        if (JSONUtil.isTypeJSON(reservedOne)) {
            this.reservedTwoList = JSONUtil.parseArray(reservedTwo);
        }
    }

    /**
     * 组装唯一标识，班级ID + 初复训标识
     */
    public void parseUniqueFlag() {
        this.uniqueFlag = this.classId + "" + this.trainFlag;
    }

    /**
     * 根据预约的考试时间判断是否可以开始考试
     */
    public void resetOk(Date exDate, JSONObject exTime) {
        JSONArray time = exTime.getJSONArray("time");
        String exYYYYMMDD = DateUtil.format(exDate, "yyyy-MM-dd");
        DateTime exStartTime = DateTime.of(exYYYYMMDD + DateUtil.format(time.getDate(0), " HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
        DateTime exEndTime = DateTime.of(exYYYYMMDD + DateUtil.format(time.getDate(1), " HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
        DateTime now = DateTime.now();
        if (now.before(exStartTime)) {
            this.ok = 1;
        } else if (now.after(exEndTime)) {
            this.ok = 2;
        }
    }
}